import services from '../service';
import _ from 'lodash';
import { Model, ItemUtils, unitsPrice } from '@/utils'
import { filterCondition } from '../config/filterConfig'
import {  tableList2 } from '../config/tableFields'
import { formEditConditions } from '../config/formFields'
import { base } from '@/config';
import { message } from 'antd'
import pageConfig from '../config/pageConfig';
const {namespace,listenRouter} = pageConfig
const initState = {
  filterCondition: filterCondition, // 列表-筛选
  formFields: _.cloneDeep(formEditConditions),
  fields: tableList2, // 列表-表格
  pagination: { // 分页
    ...base.initPage,
  },

  list: [], // 列表-数据
  filterParams: {}, // 筛选条件

  selectedRowKeys: [],  // 选择的key
  selectedRows: [],     // 选择的数据
  isCheckedAll: false,  // 是否全选


  isEnumLoading: true, // 枚举获取是否成功
}

export default Model.extend({
  namespace: namespace,
  state: _.cloneDeep(initState),
  subscriptions: {
    setup({ dispatch, listen }: any) {
      listen(listenRouter, () => {
        dispatch({
          type: 'updateState',
          payload: {
            ..._.cloneDeep(initState),
            isEnumLoading: true
          }
        })
        dispatch({ type: 'fetchSubjectList' });
      });
    }
  },
  effects: {
    // 搜索列表
    * fetchList({ payload }: any, { callWithSpinning, select, update }: any) {
      const {
        pagination: { ps, pn },
        filterParams,
      } = yield select((_: any) => _[namespace]);

      const searchParams = {
        ps,
        pn,
        ...filterParams,
        ...payload,
      }
      // const data = yield callWithSpinning(services.fetchSyncList, searchParams);
      const data = {
        datas: [{
          skuName: '2e',
          key: '1',
          id:'1'
        }],
        pn: 1,
        ps: 1,
        tc:1
      }
      if (data) {
        const { datas, pn, ps, tc } = data;
        yield update({
          list: datas,
          pagination: {
            ps,
            pn,
            total: tc,
            current: pn, // 组件需要
            pageSize: ps, // 组件需要
          },
        });
      }
    },

    // 枚举请求列表
    * fetchSubjectList({ payload }: any, { update, call, put, select }: any): any {
      const { filterParams } = yield select((_: any) => {
        return _[namespace]
      });
      // 枚举接口
      const data:any = [];
        if (data) {
        yield update({
          filterParams: {
            ...filterParams,
          },
          isEnumLoading: false
        })
        yield put('updateFilterConditon');
          yield put({
            type: 'fetchList',
          })
      }
    },
    // 添加
    * fetchSyncAdd({payload, callback}: any, { callWithConfirmLoading, put }:any) {
      const data = yield callWithConfirmLoading(services.fetchSyncAdd, { ...payload })
      callback && callback(data)
    },
  },
  reducers: {
    // 更新筛选条件配置
    updateFilterConditon(state: any, {payload}: any) {
      const {
        filterCondition,
        subjectList,
        formFields
      } = state;

      const _filterCondition = ItemUtils.getItemType(_.cloneDeep(filterCondition))
        .extend([
          {
            title: 'subjectId',
            selectCondition: subjectList,
          },
        ]).values()

      const _formFields = ItemUtils.getItemType(_.cloneDeep(formFields))
        .extend([
          {
            title: 'subjectCode',
            selectCondition: subjectList,
          },
        ]).values()

      return {
        ...state,
        filterCondition: _filterCondition,
        formFields: _formFields
      }
    },
    // 更新选择的 keys 和 rows
    updateKeysRows(state: any, {payload}:any) {
      const { selectedRowKeys, selectedRows } = payload
      return {
        ...state,
        selectedRowKeys,
        selectedRows,
      }
    },
    // 清空选择的 keys 和 rows
    clearKeysRows(state: any) {
      return {
        ...state,
        selectedRowKeys: [],
        selectedRows: [],
        isCheckedAll: false
      }
    },
  }
})